package com.controller
{
	import com.model.Model;
	import com.model.valueObjects.VOCategoryMaster;
	import com.service.CategoryMaster;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.managers.CursorManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;

	public class CategoryMasterController extends AbstractController
	{
		[Inject]
		public var categoryMasterService:CategoryMaster;
		[PostConstruct]
		public function CategoryMasterController()
		{
		}
		[EventHandler(event="CategoryMasterEvent.GET_ALL")]
		public function getAll():void{
			model.currentAdminTableIndex=9;
			sh.executeServiceCall(categoryMasterService.getAllCategories(),getAllResult, handleFault );
		}
		private function getAllResult(event:ResultEvent):void
		{
			model.categoryMasterList=event.result as ArrayCollection;
		}
		
		[EventHandler(event="CategoryMasterEvent.ADD", properties="categoryMastervo")]
		public function add(categoryMastervo:VOCategoryMaster):void
		{
			sh.executeServiceCall(categoryMasterService.addNewCategory(categoryMastervo), addResult, handleFault );	
		}
		
		private function addResult(event:ResultEvent):void
		{
			model.categoryMasterList=event.result as ArrayCollection;
			
			if(model.saveCloseBtn)model.currentForm=1;

			CursorManager.removeBusyCursor();
			Alert.show("New Category Added","Category" );
		}
		[EventHandler(event="CategoryMasterEvent.UPDATE", properties="categoryMastervo")]
		public function updateCompany(categoryMastervo:VOCategoryMaster):void
		{
			sh.executeServiceCall(categoryMasterService.editCategory(categoryMastervo), updateResult, handleFault );	
		}
		private function updateResult(event:ResultEvent):void
		{
			model.categoryMasterList=event.result as ArrayCollection;
			
			model.currentForm=1;
			CursorManager.removeBusyCursor();
			Alert.show("Details Updated ","Category");
		}
		[EventHandler(event="CategoryMasterEvent.DELETE", properties="serialnumber")]
		public function deleteR(serialnumber:int):void
		{
			//Alert.show(">>>>"+serialnumber);
			sh.executeServiceCall(categoryMasterService.deleteCategory(serialnumber), deleteResult, handleFault );	
		}
		private function deleteResult(event:ResultEvent):void
		{
			//model.categoryMasterList=event.result as ArrayCollection;
			//model.currentList=model.categoryMasterList;
			model.currentForm=1;
			getAll();
			CursorManager.removeBusyCursor();
			Alert.show("Record Deleted ","Category" );
		}
		[EventHandler(event="CategoryMasterEvent.CHECK", properties="categorycode")]
		public function checkCategoryCode(categorycode:String):void{
			sh.executeServiceCall(categoryMasterService.checkCategoryID(categorycode), checkCategoryResult, handleFault );
		}
		public function checkCategoryResult(event:ResultEvent):void{
			var res:int=event.result as int;
			if(res){
				Alert.show("Category Code Already Exist","Category");
			}
		}
		[EventHandler(event="CategoryMasterEvent.CHECKCNAME", properties="categoryName")]
		public function checkCategoryName(categoryName:String):void{
			sh.executeServiceCall(categoryMasterService.checkCategoryName(categoryName), checkCategoryNameResult, handleFault );
		}
		private function checkCategoryNameResult(event:ResultEvent):void
		{
			var res:int=event.result as int;
			if(res){
				Alert.show("Category Name Already Exist","Category");
			}
		}
		private function handleFault(event:FaultEvent):void
		{
			Alert.show( event.fault.faultDetail, "Error" );
		}
	}
}